package com.china08.yxyapi.api.manage.edu.platform.continuedu.lesson;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonItem;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonItemRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonsVideoRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.YxyKeChengUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduLessonDetailController extends YxyApiBaseController {

    @Autowired
    YxyContinueduLessonRepository conLessonRepository;

    @Autowired
    YxyContinueduLessonItemRepository itemRepository;

    @Autowired
    YxyContinueduLessonsVideoRepository videoRepository;

    @Autowired
    YxyKeChengUtil YxyKeChengUtil;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/lesson/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        String domnName = getDomainName(request);
        // 云笑播放路径
        String location = "https://yxy.res.91yunxiao.com/converted/mp4/";
        // 哈尔滨播放路径
        if (domnName.contains("hrbedu")) {
            location = "https://yxyres.hrbeduy.com/converted/mp4/";
        }

        RespModel respModel = new RespModel();
        YxyContinueduLesson lesson = conLessonRepository.findById(lessonId);
        if (null != lesson) {
            respModel.lessonId = lessonId;
            respModel.name = lesson.getName();
            respModel.summary = lesson.getSummary();
            respModel.cover = lesson.getCover();
            respModel.pv = lesson.getPv();
            respModel.status = lesson.getStatus();
            respModel.isVisible = lesson.getIsVisible();
            Date createdDate = lesson.getCreatedDate();
            Date lastModifiedDate = lesson.getLastModifiedDate();
            respModel.createDate = DateToolUtils.format(createdDate, "yyyy-MM-dd");
            respModel.lastModifiedDate = DateToolUtils.format(lastModifiedDate, "yyyy-MM-dd");
        }

        // 当前课程所有课时
        List<YxyContinueduLessonItem> itemList = itemRepository.findByLessonId(lessonId);

        Integer allDuration = 0;
        List<Pojo> list = new ArrayList<Pojo>();
        for (YxyContinueduLessonItem item : itemList) {
            Pojo pojo = new Pojo();

            String itemId = item.getId();
            pojo.itemId = itemId;
            pojo.itemName = item.getTitle();
            // 课时对应的视频id
            String videoId = item.getVideoId();
            YxyContinueduLessonsVideo video = videoRepository.findById(videoId);
            if (video != null) {
                // ####
                // 获取视频播放路径
                // 视频的md5
                String md5 = video.getMd5();
                // md5前3位
                String s03 = md5.substring(0, 3);
                // md5前3-6位
                String s36 = md5.substring(3, 6);
                // 后缀
                String suffix = ".mp4?&continueEdu_sign=" + UUID.randomUUID().toString().replace("-", "");

                String play = location + s03 + File.separator + s36 + File.separator + md5 + suffix;
                pojo.location = play;

                // ####
                // 视频时常
                Integer duration = video.getDuration();
                pojo.duration = disDuration(duration);

                // 叠加视频时常
                allDuration = allDuration + duration;

                // 获取视频状态
                pojo.videoStatus = video.getStatus();
            }
            list.add(pojo);
        }
        respModel.allDuration = disDuration(allDuration);
        respModel.itemList = list;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "课程id为空");
        }
    }

    public static class Pojo {
        public String itemName = "";
        public String itemId = "";
        public String location = "";
        public String duration = "";
        public Integer videoStatus = 0;
    }

    public static class RespModel extends YxyApiRespModel {
        public String createDate;
        public String lessonId;
        public String name;
        public String summary;
        public String cover;
        public Integer pv;
        public Integer status;
        public Integer isVisible;
        public String lastModifiedDate;
        public String allDuration = "";
        public List<Pojo> itemList;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
